package com.lt.threadTest.simpleTest;

/**
 * 简单演示串行与并行耗时示例
 */
public class ConcurrencyTest {

    private static final long count = 100000000000l;

    public static void main(String[] args) throws Exception {
        concurrency();

        serial();
    }

    /**
     * 并行调用
     * @throws Exception
     */
    private static void concurrency() throws Exception{
        long start = System.currentTimeMillis();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                long a = 0;

                for (long i = 0; i < count; i++) {
                    a += 5;
                }
            }
        });
        thread.start();

        long b = 0;

        for (long i = 0; i < count ; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;

        thread.join();
        System.out.println("并行消耗的时间："+time +"获得的次数："+b);
    }


    /**
     * 穿行
     */
    private static void serial(){
        long start = System.currentTimeMillis();

        long a = 0;

        for (long i = 0; i < count; i++) {
            a += 5;
        }
        long b = 0;

        for (long i = 0; i < count; i++) {
            b--;
        }

        long time = System.currentTimeMillis() - start;

        System.out.println("串行消耗的时间："+time +" 调用次数："+b);
    }
}
